package com.example.springbootlocal.utils;

import com.example.springbootlocal.utils.page.IPage;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author X.I.O
 * @title: ListUtils
 * @projectName spring-boot-local
 * @description: TODO
 * @date 2021/11/9 15:49
 */
public class ListUtils {

    public static <T> List<T> searchByKeyWord(List<T> tList,String field,String keyword){
        return null;
    }

    public static <T> IPage<T> pageListDeal(List<T> tList,int page,int size){
        if(tList!=null && tList.size()>0){
            Collections.reverse(tList);
        }
        int listSize=tList.size();
        int start=(page-1) * size;
        int end= (start + size) >= listSize ? listSize :(start + size);
        List<T> ts=new ArrayList<>();
        for (int i = start; i < end; i++) {
            T t=tList.get(i);
            ts.add(t);
        }
        tList=null;
        IPage<T> page1=new IPage<>(listSize,ts,page,size);
        return page1;
    }

    public static <T> List<T> filterListByField(List<T> tList,String field,Object value){
        List<T> newList=new ArrayList<>();
        if(tList==null || tList.size()==0){
            return newList;
        }
        try {
            T t=tList.get(0);
            Field field1=ReflectUtils.getFieldByName(t,field);
            for (int i = 0; i < tList.size()  ; i++) {
                T t1=tList.get(i);
                Object val=ReflectUtils.getObjectByField(t1,field1);
                if(!Strings.getString(val).equals(String.valueOf(value))){
                    newList.add(t1);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return newList;
    }

    public static <T> List<T>  getListByFieldName(List<T> tList,String fiedld,Object value){
        List<T> ts=(List<T>)getObjectByFieldName(tList,fiedld,value,true);
        return ts;
    }

    public static <T> T getObjectByFieldName(List<T> tList,String field,Object value){
        T t=(T)getObjectByFieldName(tList,field,value,false);
        return t;
    }


    /**
     * 通过属性获取索引下标
     * @param tList
     * @param field
     * @param value
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> Integer getIndexById(List<T> tList,String field,Object value) throws Exception{
        if(tList==null || tList.size()==0){
            return null;
        }
        T obi=tList.get(0);
        Field field1=ReflectUtils.getFieldByName(obi,field);
        field1.setAccessible(true);
        for (int i = 0; i < tList.size(); i++) {
            T obj_i=tList.get(i);
            Object var=field1.get(obj_i);
            if(Strings.getString(var).equals(String.valueOf(value))){
                return i;
            }
        }
        return null;
    }


    /**
     * 通过属性名进行搜索
     * @param tList 对象
     * @param field 属性
     * @param value 值
     * @param <T> 泛型
     * @return
     */
    private static <T> Object getObjectByFieldName(List<T> tList,String field,Object value,Boolean isMore){
        if(tList==null || tList.size()==0){
            return tList;
        }
        List<T> ts=null;
        if(isMore){
            ts=new ArrayList<>(tList.size());
        }
        T t=tList.get(0);
        Field field1=ReflectUtils.getFieldByName(t,field);
        if(field1==null){
            return tList;
        }
        int size=tList.size();
        try {
            for (int i = 0; i < size; i++) {
                T t1=tList.get(i);
                Object val=ReflectUtils.getObjectByField(t1,field1);
                if(Strings.getString(val).equals(String.valueOf(value))){
                    if(isMore){
                        ts.add(t1);
                    }else {
                        return t1;
                    }
                }
            }
            if(isMore){
                tList=null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        if (isMore){
            return ts;
        }else {
            return null;
        }
    }

    public static <T> IPage<T> getLikeListByKeyWord(List<T> tList,String keyword,int page,int size){
        List<T> ts=getLikeListByKeyWord(tList,keyword);
        tList=null;
        return pageListDeal(ts,page,size);
    }

    /**
     * 模糊匹配是否有条件满足
     * @param tList
     * @param keyword
     * @param <T>
     * @return
     */
    public static <T> List<T> getLikeListByKeyWord(List<T> tList,String keyword){
        List<T> ts=new ArrayList<>();
        if(tList==null || tList.size()==0){
            return ts;
        }
        if(keyword==null){
            return tList;
        }
        T obj=tList.get(0);
        try {
            Field[] fields=ReflectUtils.getFields(obj);
            for (int i = 0; i < tList.size() ; i++) {
                T obj_var=tList.get(i);
                //模糊查询属性的值是否有满足条件的
                Boolean flag=getObjByKeyword(fields,obj_var,keyword);
                if(flag){
                    ts.add(obj_var);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ts;
    }


    /**
     * 模糊查询属性的值是否有满足条件的
     * @param fields
     * @param t
     * @param keyword
     * @param <T>
     * @return
     * @throws Exception
     */
    private static <T> Boolean getObjByKeyword(Field[] fields,T t,String keyword) throws Exception{
        for (int i = 0; i < fields.length ; i++) {
            Field field=fields[i];
            field.setAccessible(true);
            Object var=field.get(t);
            if(Strings.getString(var).contains(String.valueOf(keyword))){
                return true;
            }
        }
        return false;
    }







}
